/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.broker.ui.async.gui.graphical.actions;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JOptionPane;

import org.ourgrid.broker.ui.async.client.BrokerAsyncInitializer;
import org.ourgrid.common.spec.main.CompilerException;
import org.ourgrid.common.ui.gui.OGFileChooser;

public class SetGridAction extends AbstractBrokerAction {

	private static final long serialVersionUID = 1L;

	public static final String GDF_FILE_DESCR = "Grid Description File (*.gdf)";

	public static final String GDF_FILE_EXT = ".gdf";
	
	private static final String ACTION_NAME = "Set grid";

	private final String gdf;

	private Component view;


	public SetGridAction( Component view ) {
		this( view, null );
	}


	public SetGridAction( Component view, String gdf ) {
		this(getActionName(gdf), gdf);
		this.view = view;
	}
	
	private static String getActionName(String gdf) {
		if ( gdf == null ) {
			return ACTION_NAME;
		}
		return ACTION_NAME + " - " + new File(gdf).getName();
	}


	public SetGridAction(String title, String gdf ) {
		super(title);
		this.gdf = gdf;
	}


	public void actionPerformed( ActionEvent e ) {
		File gdfFile;
		String gdf = null;
		boolean addToHistory = true;
		if (this.gdf == null) {
			gdfFile = new OGFileChooser( GDF_FILE_DESCR, GDF_FILE_EXT ).selectFile( this.view );
			if (gdfFile == null) {
				return;
			}
			gdf = gdfFile.getPath();
		} else {
			gdf = this.gdf;
			addToHistory = false;
		}
		
		
		try {
			BrokerAsyncInitializer.getInstance().getComponentClient().setGrid(gdf, addToHistory);
		} catch (CompilerException e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage(), "Error on gdf compilation", 
					JOptionPane.ERROR_MESSAGE);
		}
	}


	@Override
	public int hashCode() {

		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((this.gdf == null) ? 0 : this.gdf.hashCode());
		return result;
	}


	@Override
	public boolean equals( Object obj ) {

		if ( this == obj )
			return true;
		if ( obj == null )
			return false;
		if ( getClass() != obj.getClass() )
			return false;
		final SetGridAction other = (SetGridAction) obj;
		if ( this.gdf == null ) {
			if ( other.gdf != null )
				return false;
		} else if ( !this.gdf.equals( other.gdf ) )
			return false;
		return true;
	}

}
